<template>
  <CustomDialog :drag-dialog="dialogInfo" style="z-index:1201;">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-form-item label="受权人" prop="busMainData.authUserAccount">
        <UserSelectDialog v-model="form.busMainData.authUserName" :multi-select="false" @userSelectCallback="userSelectCallback"/>
      </el-form-item>
      <el-form-item label="授权起始日期" prop="busMainData.beginDate">
        <el-date-picker
          v-model="form.busMainData.beginDate"
          placeholder="授权起始日期"
          type="date"
          size="mini"
          clearable
          style="width: 200px"
          class="filter-item" />
      </el-form-item>
      <el-form-item label="授权截止日期" prop="busMainData.endDate">
        <el-date-picker
          v-model="form.busMainData.endDate"
          placeholder="授权截止日期"
          type="date"
          size="mini"
          clearable
          style="width: 200px"
          class="filter-item" />
      </el-form-item>
      <el-form-item label="是否启用" prop="busMainData.validFlag">
        <el-radio v-for="item in dicts.enable_flag" :key="item.id" v-model="form.busMainData.validFlag" :label="item.dictCode">{{ item.dictName }}</el-radio>
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','WORKFLOW_AUTH_ALL','WORKFLOW_AUTH_CREATE']" :loading="loading" type="primary" @click="doSave">保存</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import workflowAuth from '@/api/system/WorkflowAuth'
import UserSelectDialog from '@/views/components/UserSelectDialog'

export default {
  components: { UserSelectDialog },
  dictTypes: ['enable_flag'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      dialogInfo: {
        name: 'workflowAuthMngFormDialog',
        title: '授权审批',
        fullScreen: false,
        width: 500,
        height: 50
      },
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.authUserAccount': [
          { required: true, message: '请选择受权人', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore() {
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      workflowAuth.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      workflowAuth.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      workflowAuth.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      workflowAuth.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      workflowAuth.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      workflowAuth.toChange(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    userSelectCallback(selectedRow) {
      this.form.busMainData.authUserAccount = ''
      this.form.busMainData.authUserName = ''
      if (selectedRow) {
        this.form.busMainData.authUserAccount = selectedRow.userAccount
        this.form.busMainData.authUserName = selectedRow.userName
      }
    }
  }
}
</script>
